<template>
  <div class="card-box">
    <div class="card-title">
      <span>政府投资项目</span>
      <el-col :span="2" v-show="showButton">
        <el-button type="text" @click="addRow">新增</el-button>|
        <el-button type="text" @click="delRow">删除</el-button>
      </el-col>
    </div>
    <div class="margin-box">
      <el-table :data="commitSelectRow" align="center" v-if="commitSelect">
        <el-table-column label="建设单位" prop="constructionUnit"></el-table-column>
        <el-table-column label="建设开始日期" prop="constructionStartTime" :formatter="dataFormat"></el-table-column>
        <el-table-column label="竣工日期" prop="completedTime" :formatter="dataFormat"></el-table-column>
        <el-table-column label="资产占用单位" prop="assetOccupyingUnit"></el-table-column>
        <el-table-column label="上级主管单位" prop="superiorCompetentUnit"></el-table-column>
        <el-table-column label="资产使用单位" prop="assetUseUnit"></el-table-column>
      </el-table>
    </div>
    <v-dialog title="选择政府投资项目" :show.sync="dialogVisible" :close-on-click-modal="false" width="1200px">
      <div style="padding: 5px">
        <dataList :pageInfo="pager" :listData="tableData" placeholder="请输入资产名称" searchName="keyword" :menuList="[]" :filterList="filterList">
          <el-table :data="tableData">
            <el-table-column label="资产占用单位" prop="assetOccupyingUnit" align="center"></el-table-column>
            <el-table-column label="填表日期" prop="registerDate" align="center"></el-table-column>
            <el-table-column label="登记单位" prop="registrationUnit" align="center"></el-table-column>
            <el-table-column label="建设单位" prop="constructionUnit" align="center"></el-table-column>
            <el-table-column label="项目建设地址" prop="constructionAddress" align="center"></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="handleClickAdd(scope.row)">添加</el-button>
              </template>
            </el-table-column>
          </el-table>
        </dataList>
      </div>
    </v-dialog>
  </div>
</template>

<script>
import { getGovernmentPage, getAssetRegistration } from '../api/propertietar'
let moment = require('moment')
export default {
  name: "new-government",
  props: {
    showButton: {
      type: Boolean,
      default: true
    }
  },
  provide () {
    return {
      initList: this.getGovernmentList
    };
  },
  beforeMount () {
    this.getGovernmentList()
  },
  data () {
    return {
      dialogVisible: false,
      commitSelect: true,
      commitSelectRow: [],
      params: {
        current: 1,
        size: 20,
        keyword: ""
      },
      filterList: [{ label: "填表日期", inputType: "daterange", name: ["registerDateStart", "registerDateEnd"] }],
      pager: {
        total: 0, // 总页数
        current: 1, // 当前页数
        size: 20 // 每页显示多少条
      },
      //表单数据
      tableData: []
    }
  },
  methods: {
    //新增
    addRow () {
      if (this.governmentInvestmentId === '') {
        this.$alert('暂无政府投资项目', '提示', {
          confirmButtonText: '确定',
        })
      } else {
        this.dialogVisible = true
      }
    },
    //删除
    delRow () {
      this.$confirm('是否删除政府投资项目?', '提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        this.commitSelectRow = []
        this.$message.success('删除成功！')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    //格式化
    dataFormat: function (row, column, cellValue) {
      if (!cellValue) {
        return ''
      }
      return moment(cellValue).format('YYYY-MM-DD')
    },
    //添加
    handleClickAdd (item) {
      if (this.commitSelectRow.length > 0) {
        this.$confirm('一次只能添加一条数据，该操作将覆盖上条数据,是否继续?', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        }).then(() => {
          this.commitSelect = false
          this.$nextTick(() => {
            this.commitSelect = true
            this.commitSelectRow[0] = item
          })
          this.$emit('handleSelectRow', { 'list': this.commitSelectRow })
          this.$message.success('添加成功！')
          this.dialogVisible = false
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      } else {
        this.commitSelectRow.push(item)
        this.$emit('handleSelectRow', { 'list': this.commitSelectRow })
        this.$message.success('添加成功！')
        this.dialogVisible = false
      }
    },
    //  * 查询列表
    getGovernmentList (params = {}) {
      this.loading = true
      let requestData = { ...this.params, ...params }
      getGovernmentPage(requestData).then(res => {
        const { current, size, total, records } = res.data.data
        this.tableData = records;
        this.params = requestData;
        this.pager = { total, current, size };
      }).catch(error => {
        this.$message({
          type: "error",
          message: "查询失败"
        });
      }).finally(() => (this.loading = false))
    },
    // hxf-0923-政府联动出来
    handleChangeAssetRegistration (val) {
      // console.log('val',val)
      getAssetRegistration(val).then(res => {
        if (res.data.code === 0) {
          this.commitSelectRow = [res.data.data]
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
